

namespace Dotnet.Script.ScriptPath;

class PathHandle
{
    private static readonly string[] ExcludeFolderNames =
    [
        "\\venv\\",
        "\\venv",
        "\\node_modules\\",
        "\\node_modules"
    ];

    private static readonly string[] ExcludeFilesNames = [".DS_Store"];
    private static string _srcDirPath;
    private static readonly DateTime _now = DateTime.Now;



    public static string SrcDirPath
    {
        get { return _srcDirPath; }
        set { _srcDirPath = value; }
    }

    public static string GetNewFilePath(string oldFilePath)
    {
        var newFilePath = Path.Combine(
            PathHandle.DstDirPath,
            oldFilePath.Split(PathHandle.SrcDirPath + "\\").Last()
        );
        return newFilePath;
    }

    public static string DstDirPath
    {
        get
        {
            string folderPath =
                Path.GetDirectoryName(_srcDirPath) ?? throw new Exception("无法项目文件夹的父级文件夹");
            string? folderName = Path.GetFileName(_srcDirPath);
            var newFolderName = $"{folderName}_{_now:yyyyMMdd_HHmmss}";
            var newFolderPath = Path.Combine(folderPath, newFolderName);
            return newFolderPath;
        }
    }

    public static void SetSrcDirPath(string[] args)
    {
        SrcDirPath = args.Count() > 0 ? args[0] : throw new ArgumentException("请传入要处理的文件路径");
        // DstDirPath = "D:\\share\\codelearn\\codelearn\\v-dashboard-master";
    }

    public static async Task<List<string>> GetFilesAsync()
    {
        var files = new List<string>();
        List<string> dirs = ListDirsAndSubdirectories(PathHandle.SrcDirPath);

        // foreach (var dir in dirs)
        // {
        //     files.AddRange(ListFilesNoSubdirectories(dir));
        // }
        List<Task<List<string>>> tasks = new List<Task<List<string>>>();
        foreach (var dir in dirs)
        {
            tasks.Add(Task.Run(() => ListFilesNoSubdirectories(dir)));
        }
        await Task.WhenAll(tasks);
        foreach (var task in tasks)
        {
            files.AddRange(task.Result);
        }

        files.AddRange(ListFilesNoSubdirectories(PathHandle.SrcDirPath));
        return files;
    }

    static List<string> ListDirsAndSubdirectories(string folderPath)
    {
        var dirs = new List<string>();
        foreach (
            string dir in Directory.GetDirectories(folderPath, "*", SearchOption.AllDirectories)
        )
        {
            if (ExcludeDirFlag(dir))
                continue;

            dirs.Add(dir);
        }
        return dirs;
    }

    static bool ExcludeDirFlag(string dir)
    {
        foreach (var ExcludeFolderName in ExcludeFolderNames)
        {
            if (dir.Contains(ExcludeFolderName))
            {
                return true;
            }
        }
        return false;
    }

    public static List<string> ListFilesAndSubdirectories(string folderPath)
    {
        var files = new List<string>();
        foreach (string file in Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories))
        {
            if (ExcludeFileFlag(file))
                continue;

            files.Add(file);
        }
        return files;
    }

    public static List<string> ListFilesAndSubdirectoriesOrderedByLastModified(string folderPath)
    {
        var files = new List<string>();
        foreach (string file in Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories))
        {
            if (ExcludeFileFlag(file))
                continue;

            files.Add(file);
        }
        var sortedFiles = files
            .OrderByDescending(File.GetLastWriteTimeUtc)
            .ToList(); // 使用Utc时间避免时区问题（可选）
        return sortedFiles; // 返回排序后的文件列表
    }




    static bool ExcludeFileFlag(string file)
    {
        foreach (var ExcludeFilesName in ExcludeFilesNames)
        {
            if (file.Contains(ExcludeFilesName))
            {
                return true;
            }
        }
        return false;
    }

    public static List<string> ListFilesNoSubdirectories(string folderPath)
    {
        var files = new List<string>();
        foreach (string file in Directory.GetFiles(folderPath, "*", SearchOption.TopDirectoryOnly))
        {
            if (ExcludeFileFlag(file))
                continue;
            files.Add(file);
        }
        return files;
    }

    public static List<string> ListDirsNoSubdirectories(string folderPath)
    {
        var dirs = new List<string>();
        foreach (
            string dir in Directory.GetDirectories(folderPath, "*", SearchOption.TopDirectoryOnly)
        )
        {
            if (ExcludeDirFlag(dir))
                continue;

            dirs.Add(dir);
        }
        return dirs;
    }
}

